<?php
namespace Ensao\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Ensao\AppBundle\Form\DossierType;
use Ensao\AppBundle\Entity\Dossier;
use Ensao\AppBundle\Form\PaiementType;
use Ensao\AppBundle\Entity\Paiement;
use Ensao\AppBundle\Form\ProcedType;
use Ensao\AppBundle\Entity\Proced;
use Ensao\AppBundle\Entity\Instance;
class DossierController extends Controller
{
    public function indexAction()
    {
		$repository = $this->getDoctrine()
                            ->getRepository('EnsaoAppBundle:Dossier');

          $liste = $repository->findAll();

	       foreach($liste as $l)
               $idd=$l->getId();
               $iddd=$idd - 9;

        return $this->render('EnsaoAppBundle:Dossier:index.html.twig',array( 'liste'=> $liste ,'idd'=>$iddd));
    }

    public function rechercherAction()
    {

        if( $this->get('request')->getMethod() == 'POST' )
        {
            $id = $this->get('request')->get('dossier_num');
            $repository = $this->getDoctrine()
                ->getRepository('EnsaoAppBundle:Dossier');
            $dossier=$repository->find($id);
            if(! $dossier){
                $this->get('session')->setFlash("notice","هذا الملف غير موجود ");
                return  $this->redirect($this->generateUrl('dossier_accueil'));
            }
            return $this->redirect($this->generateUrl('dossier_affiche',array('id' =>$id)));
        }

    }

    public function afficherAction($id)
    {

        $repository = $this->getDoctrine()
            ->getRepository('EnsaoAppBundle:Dossier');
        $dossier=$repository->find($id);

        $listProceds=$dossier->getProceds();

        $ssCatg=$dossier->getSousCategorie();
        $listClient= $dossier->getClients();
        $listAdversaire= $dossier->getAdversaires();
        $listPaiements=$dossier->getPaiements();
        //nombre de paiments
        $i=0;
        foreach ($listPaiements as $elt){
            $i=$i+1;
        }
        //nombre de proceds
        $j=0;
        foreach ($listProceds as $elt){
            $j=$j+1;
        }
        //forms pour paiement
        $formPaie = $this->createForm(new PaiementType());
        $formModifPaie = $this->createForm(new PaiementType());
         //forms pour procedure
        $formProced1=  $this->createForm(new ProcedType());
        $formProced2=  $this->createForm(new ProcedType());
        $formModifProced=$this->createForm(new ProcedType());

        return $this->render('EnsaoAppBundle:Dossier:afficher.html.twig',array( 'j'=>$j,'i'=>$i,'dossier' => $dossier,
            'formPaie' => $formPaie->createView(),'formModifPaie'=>$formModifPaie->createView(),'formProced1'=>$formProced1->createView(),
            'formProced2'=>$formProced2->createView(),'formModifProced'=>$formModifProced->createView(),'ssCateg'=> $ssCatg,'listClient'=> $listClient,
            'listAdversaire'=>$listAdversaire,'listPaiements'=>$listPaiements,'listPaiements'=>$listPaiements ,'listProceds'=>$listProceds) );
    }

    public function rechercheAvanceeAction()
    {
        return $this->render('EnsaoAppBundle:Dossier:rechercheAvancee.html.twig');
    }

    // editer= ajout $param ou modif $pr
    public function editerProcedAction($id,$param=null,$pr=null){

        $dossier=$this->getDoctrine()->getRepository('EnsaoAppBundle:Dossier')->find($id);
        if (isset($pr))
        {
            // modification d'une procedure existante : on recherche ses données
            $proced =$this->getDoctrine()->getRepository('EnsaoAppBundle:Proced')->find($pr);

            if (!$proced)
            {
                throw $this->createNotFoundException('procedure introuvable ');
            }
        }
        elseif(isset($param))
        {
            // ajout d'un nouvelle procedure
            $proced=new Proced();
            $instances=$dossier->getInstances() ;
            foreach($instances as $instancee){
                if($param==1){
                    if($instancee->getLibelle()=='إبتدائية') {
                        $instance=$instancee;
                    }
                }
                elseif($param==2){
                    if($instancee->getLibelle()=='إستئنافية') {
                        $instance=$instancee;
                    }
                }
                elseif(($param==3)){
                    if($instancee->getLibelle()=='النقض') {
                        $instance=$instancee;
                    }
                }
            }

            if (!$dossier ||!$instance ) {
                //message d'erreur
                $this->get('session')->setFlash("notice2","المرجو مراجعة المعلومات");
                return $this->redirect($this->generateUrl('dossier_affiche',array('id' =>$id)));
            }

            $proced->setDossier($dossier);
            $proced->setInstance($instance);

        }

        $em = $this->getDoctrine()->getEntityManager();
        $form = $this->get('form.factory')->create(new ProcedType(), $proced);

        $request = $this->get('request');

       if ($request->getMethod() == 'POST'){

            $form->bindRequest($request);

           if ($form->isValid())  {

               if (isset($param))
               {
                   $em->persist($instance);
               }

                $em->persist($proced);
                $em->flush();
                $this->get('session')->setFlash("notice","تمت العملية بنجاح");

               if (isset($pr))
               {
                   $this->get('session')->setFlash("notice","^____^ تمت إضافة التغييرات !");
               }
               elseif(isset($param))
               {
                   $this->get('session')->setFlash("notice","تمت العملية بنجاح");
               }

            }
        }
        //return new Response(var_dump($form->getErrors()));
        return $this->redirect($this->generateUrl('dossier_affiche',array('id' =>$id)));

    }
         //ajout + modification des paiements
    public function editerOperationAction($id,$op=null)
    {
        $request = $this->get('request');

        $dossier=$this->getDoctrine()->getRepository('EnsaoAppBundle:Dossier')->find($id);
        if (!$dossier) {
            //message d'erreur
            $this->get('session')->setFlash("notice2","المرجو مراجعة المعلومات");
            return $this->redirect($this->generateUrl('dossier_affiche',array('id' =>$id)));
        }

        if (isset($op))
        {
            // modification d'un paiement existant : on recherche ses données
            $paiement =$this->getDoctrine()->getRepository('EnsaoAppBundle:Paiement')->find($op);

            if (!$paiement)
            {
                throw $this->createNotFoundException('paiement introuvable ');
            }
        }
        else
        {
            // ajout d'un nouveau paiement
            $paiement=new Paiement();
            $paiement->setDossier($dossier);

        }

            $em = $this->getDoctrine()->getEntityManager();
            $form = $this->get('form.factory')->create(new PaiementType(), $paiement);


            if ($request->getMethod() == 'POST'){
                $form->bindRequest($request);
                if ($form->isValid())  {

                    $em->persist($paiement);
                    $em->flush();

                    if (isset($op))
                    {
                        $this->get('session')->setFlash("notice","^____^ تمت إضافة التغييرات !");
                    }
                    else
                    {
                        $this->get('session')->setFlash("notice","تمت العملية بنجاح");
                    }

                }

            }
            return $this->redirect($this->generateUrl('dossier_affiche',array('id' =>$id)));
           // return new Response(var_dump($form->getErrors()));
    }

     // suppression paiement selon $op et procedure selon $pr
    public function deleteAction($id,$pr=null,$op=null)
    {
        $em = $this->container->get('doctrine')->getEntityManager();

        if (isset($op))
        {
            $paiement =$this->getDoctrine()->getRepository('EnsaoAppBundle:Paiement')->find($op);

            if (!$paiement)
            {
                $this->get('session')->setFlash("notice2","المرجو مراجعة المعلومات");
            }
            $em->remove($paiement);
            $this->get('session')->setFlash("notice","تمت عملية الحذف");
        }
        elseif(isset($pr))
        {
            $proced =$this->getDoctrine()->getRepository('EnsaoAppBundle:Proced')->find($pr);

            if (!$proced)
            {
                $this->get('session')->setFlash("notice2","المرجو مراجعة المعلومات");
            }
            $em->remove($proced);
            $this->get('session')->setFlash("notice","تمت عملية الحذف");

        }

        $em->flush();

        return $this->redirect($this->generateUrl('dossier_affiche',array('id' =>$id)));
    }


}

?>